<?php
namespace app\system\controller;

use app\common\model\CmsFieldModel;
use app\common\model\CmsMoudleModel;
use think\Request;

class CmsFieldController extends BaseController
{

    private $cmsMoudleModel;
    private $cmsFieldMoudle;
    private $mid;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->mid            = $this->request->param('mid/d', 0);
        $this->cmsFieldMoudle = new CmsFieldModel();
        $this->cmsMoudleModel = new CmsMoudleModel();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $list             = $this->cmsFieldMoudle->where('mid', $this->mid)->order('field_sort asc')->select();
        $noAllowDelete    = $this->cmsFieldMoudle->noAllowDelete();
        $noAllowEdit      = $this->cmsFieldMoudle->noAllowEdit();
        $noAllowForbidden = $this->cmsFieldMoudle->noAllowForbidden();;
        $this->assign('mid', $this->mid);
        $this->assign('list', $list);
        $this->assign('noAllowDelete', $noAllowDelete);
        $this->assign('noAllowEdit', $noAllowEdit);
        $this->assign('noAllowForbidden', $noAllowForbidden);
        return $this->fetch();
    }

    /**
     * 添加字段
     * @return mixed
     */
    public function add()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'CmsFieldValidate.add');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->cmsFieldMoudle->addData($data)) {
                $this->cmsFieldMoudle->updateCache($this->mid);
                $this->success('添加成功！', url('index', ['mid' => $this->mid]));
            } else {
                $this->error($this->cmsFieldMoudle->getError());
            }
        }
        $moudelInfo = $this->cmsMoudleModel->get($this->mid);
        $validRule  = $this->cmsFieldMoudle->getValidateRule();
        $this->assign('validRule', $validRule);
        $this->assign('moudleInfo', $moudelInfo);
        return $this->fetch();
    }

    /**
     * 修改字段
     * @return mixed
     */
    public function edit()
    {
        if ($this->request->isAjax()) {
            $data   = $this->request->param();
            $result = $this->validate($data, 'CmsFieldValidate.edit');
            if (true !== $result) {
                $this->error($result);
            }
            if ($this->cmsFieldMoudle->editData($data)) {
                $this->cmsFieldMoudle->updateCache($this->mid);
                $this->success('修改成功！', url('index', ['mid' => $this->mid]));
            } else {
                $this->error($this->cmsFieldMoudle->getError());
            }
        }
        $fid        = $this->request->param('fid/d', 0);
        $info       = $this->cmsFieldMoudle->get($fid);
        $moudleInfo = $this->cmsMoudleModel->get($info['mid']);
        $validRule  = $this->cmsFieldMoudle->getValidateRule();
        $this->assign('moudleInfo', $moudleInfo);
        $this->assign('validRule', $validRule);
        $this->assign('info', $info);
        $setField = $this->fetch("cms_field/data/" . $info['field_type'] . "/edit");
        $this->assign('setField', $setField);
        return $this->fetch();
    }

    /**
     * 删除字段
     */
    public function del()
    {
        $fid = $this->request->param('fid', 0);
        if ($this->cmsFieldMoudle->delData($fid)) {
            $this->cmsFieldMoudle->updateCache($this->mid);
            $this->success('删除成功！');
        } else {
            $this->error($this->cmsFieldMoudle->getError());
        }
    }

    /**
     * 获取字段类型
     */
    public function getFieldTpl()
    {
        $action    = $this->request->param('action');
        $fieldType = $this->request->param('fieldType');
        return $this->fetch("cms_field/Data/{$fieldType}/{$action}");
    }

    /**
     * 更新排序
     */
    public function updateSort()
    {
        $fieldSort = $this->request->param('fieldSort/a');
        foreach ($fieldSort as $k => $v) {
            $dataList[] = ['fid' => $k, 'field_sort' => $v];
        }
        $this->cmsFieldMoudle->saveAll($dataList);
        $this->cmsFieldMoudle->updateCache($this->mid);
        $this->success('排序更新成功！');
    }

    /**
     * 启用禁用
     */
    public function forbidden()
    {
        $fid    = $this->request->param('fid/d', 0);
        $enable = $this->request->param('enable/d', 0);
        $this->cmsFieldMoudle->where('fid', $fid)->setField('enable', $enable);
        $this->cmsFieldMoudle->updateCache($this->mid);
        $this->redirect('index', ['mid' => $this->mid]);
    }

    /**
     * 更新字段缓存
     */
    public function updateCache()
    {
        $this->cmsFieldMoudle->updateCache($this->mid);
        $this->success('更新字段缓存成功！');
    }

}
